//
//  NSString+HLQRegex.m
//  e_card
//
//  Created by gynjobs on 2017/5/18.
//  Copyright © 2017年 haodai. All rights reserved.
//

#import "NSString+HLQRegex.h"

@implementation NSString (HLQRegex)

//判断字符串是否为空
+ (BOOL) isBlankStringWithString:(NSString *)string{
    if (string == nil || string == NULL) {
        return YES;
    }
    if ([string isKindOfClass:[NSNull class]]) {
        return YES;
    }
    if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
        return YES;
    }
    if ([string containsString:@"null"]) {
        return YES;
    }
    return NO;
}
/** 是否是URLString **/
- (BOOL)isUrlString

{
    if(self == nil)
        
        return NO;
    
    NSString *url;
    
    if (self.length>4 && [[self substringToIndex:4] isEqualToString:@"www."]) {
        
        url = [NSString stringWithFormat:@"http://%@",self];
        
    }else{
        
        url = self;
        
    }
    
    NSString *regex =@"[a-zA-z]+://[^\\s]*";
    NSPredicate *urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    return [urlTest evaluateWithObject:self];
    
}

- (CGSize)sizeWithfont:(UIFont *)font maxWidth:(CGFloat)maxW {
    NSMutableDictionary *attrs = [[NSMutableDictionary alloc] init];
    
    attrs[NSFontAttributeName] = font;
    CGSize maxSize = CGSizeMake(maxW, MAXFLOAT);
    return [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}
- (CGSize)sizeWithFont:(UIFont *)font byHeight:(CGFloat)height
{
    //	return [self sizeWithFont:font
    //			constrainedToSize:CGSizeMake(999999.0f, height)
    //				lineBreakMode:UILineBreakModeWordWrap];
    //    return  [self sizeWithFont:font
    //             constrainedToSize:
    //             CGSizeMake(999999.0f, height)
    //                 lineBreakMode:NSLineBreakByTruncatingTail];
    
    NSMutableDictionary *attrs = [[NSMutableDictionary alloc] init];
    
    attrs[NSFontAttributeName] = font;
    CGSize maxSize = CGSizeMake(999999.0f, height);
    return [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
    
}
- (CGSize)sizeWithfont:(UIFont *)font {
    
    return [self sizeWithfont:font maxWidth:0];
}


// 验证纯数字
+(BOOL)validatePureNum:(NSString *)string{
    
    string = [string stringByTrimmingCharactersInSet:[NSCharacterSet decimalDigitCharacterSet]];
    
    if(string.length > 0)
    {
        
        return NO;
        
    }
    return YES;
    
    
}


+(BOOL)validatePureZeros:(NSString*)value{
    
    NSString * fstr = [value stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"0"]];
    
    if(fstr.length > 0){
        
        return NO;
    }
    
    return YES;
    
}


//验证邮箱

+ (BOOL) validateEmail:(NSString *)email{
    
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:email];
    
}

//手机号码验证

+ (BOOL) validateMobile:(NSString *)mobile{
    
    
    NSString * phone_num = @"^(13|14|15|17|18)\\d{9}$";
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phone_num];
    
    if([regextestmobile evaluateWithObject:mobile] == YES){
        
        return YES;
        
    }else{
        
        return NO;
    }
    
    
}

//车牌号验证

+ (BOOL) validateCarNo:(NSString *)carNo{
    
    NSString *carRegex = @"^[\u4e00-\u9fa5]{1}[a-zA-Z]{1}[a-zA-Z_0-9]{4}[a-zA-Z_0-9_\u4e00-\u9fa5]$";
    NSPredicate *carTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",carRegex];
    GYNLog(@"carTest is %@",carTest);
    return [carTest evaluateWithObject:carNo];
    
}

//用户名验证

+ (BOOL) validateUserName:(NSString *)name{
    
    NSString *userNameRegex = @"^[A-Za-z0-9]{6,20}+$";
    NSPredicate *userNamePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",userNameRegex];
    BOOL B = [userNamePredicate evaluateWithObject:name];
    return B;
    
    
}

//用户昵称验证

+ (BOOL) validateNickname:(NSString *)nickname{
    
    NSString *nicknameRegex = @"^[\u4e00-\u9fa5]{2,20}$";
    NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",nicknameRegex];
    return [passWordPredicate evaluateWithObject:nickname];
    
    
}

//密码验证

+ (BOOL) validatePassword:(NSString *)passWord{
    
    NSString *passWordRegex = @"^[a-zA-Z0-9]{6,20}+$";
    NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",passWordRegex];
    return [passWordPredicate evaluateWithObject:passWord];
    
}

//身份证号验证

+ (BOOL) validateIdentityCard: (NSString *)value{
    value = [value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
    NSInteger length =0;
    
    if (!value) {
        
        return NO;
        
    }else {
        
        length = [value length];
        
        if (length !=15 && length !=18) {
            
            return NO;
            
        }
        
    }
    
    // 省份代码
    
    NSArray *areasArray =@[@"11",@"12", @"13",@"14", @"15",@"21", @"22",@"23", @"31",@"32", @"33",@"34", @"35",@"36", @"37",@"41", @"42",@"43", @"44",@"45", @"46",@"50", @"51",@"52", @"53",@"54", @"61",@"62", @"63",@"64", @"65",@"71", @"81",@"82", @"91"];
    
    
    NSString *valueStart2 = [value substringToIndex:2];
    
    BOOL areaFlag =NO;
    
    for (NSString *areaCode in areasArray) {
        
        if ([areaCode isEqualToString:valueStart2]) {
            
            areaFlag =YES;
            
            break;
            
        }
        
    }
    
    
    if (!areaFlag) {
        
        return NO;
        
    }
    
    
    NSRegularExpression *regularExpression;
    NSUInteger numberofMatch;
    
    int year =0;
    
    switch (length) {
            
        case 15:
            
            year = [value substringWithRange:NSMakeRange(6,2)].intValue +1900;
            if (year %4 ==0 || (year %100 ==0 && year %4 ==0)) {
                
                regularExpression = [[NSRegularExpression alloc]initWithPattern:@"^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$"
                                     
                                                                        options:NSRegularExpressionCaseInsensitive
                                     
                                                                          error:nil];//测试出生日期的合法性
                
            }else {
                
                regularExpression = [[NSRegularExpression alloc]initWithPattern:@"^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$"
                                     
                                                                        options:NSRegularExpressionCaseInsensitive
                                     
                                                                          error:nil];//测试出生日期的合法性
                
            }
            
            numberofMatch = [regularExpression numberOfMatchesInString:value
                             
                                                               options:NSMatchingReportProgress
                             
                                                                 range:NSMakeRange(0, value.length)];
            
            
            if(numberofMatch >0) {
                
                return YES;
                
            }else {
                
                return NO;
                
            }
            
        case 18:
            
            
            year = [value substringWithRange:NSMakeRange(6,4)].intValue;
            
            if (year %4 ==0 || (year %100 ==0 && year %4 ==0)) {
                
                regularExpression = [[NSRegularExpression alloc]initWithPattern:@"^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]$"
                                     
                                                                        options:NSRegularExpressionCaseInsensitive
                                     
                                                                          error:nil];//测试出生日期的合法性
                
            }else {
                
                regularExpression = [[NSRegularExpression alloc]initWithPattern:@"^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$"
                                     
                                                                        options:NSRegularExpressionCaseInsensitive
                                     
                                                                          error:nil];//测试出生日期的合法性
                
            }
            
            numberofMatch = [regularExpression numberOfMatchesInString:value
                             
                                                               options:NSMatchingReportProgress
                             
                                                                 range:NSMakeRange(0, value.length)];
            
            
            if(numberofMatch >0) {
                
                int S = ([value substringWithRange:NSMakeRange(0,1)].intValue + [value substringWithRange:NSMakeRange(10,1)].intValue) *7 + ([value substringWithRange:NSMakeRange(1,1)].intValue + [value substringWithRange:NSMakeRange(11,1)].intValue) *9 + ([value substringWithRange:NSMakeRange(2,1)].intValue + [value substringWithRange:NSMakeRange(12,1)].intValue) *10 + ([value substringWithRange:NSMakeRange(3,1)].intValue + [value substringWithRange:NSMakeRange(13,1)].intValue) *5 + ([value substringWithRange:NSMakeRange(4,1)].intValue + [value substringWithRange:NSMakeRange(14,1)].intValue) *8 + ([value substringWithRange:NSMakeRange(5,1)].intValue + [value substringWithRange:NSMakeRange(15,1)].intValue) *4 + ([value substringWithRange:NSMakeRange(6,1)].intValue + [value substringWithRange:NSMakeRange(16,1)].intValue) *2 + [value substringWithRange:NSMakeRange(7,1)].intValue *1 + [value substringWithRange:NSMakeRange(8,1)].intValue *6 + [value substringWithRange:NSMakeRange(9,1)].intValue *3;
                
                int Y = S %11;
                
                NSString *M =@"F";
                
                NSString *JYM =@"10X98765432";
                
                M = [JYM substringWithRange:NSMakeRange(Y,1)];// 判断校验位
                
                if ([M isEqualToString:[value substringWithRange:NSMakeRange(17,1)]]) {
                    
                    return YES;// 检测ID的校验位
                    
                }else {
                    
                    return NO;
                    
                }
                
                
            }else {
                
                return NO;
                
            }
            
        default:
            
            return NO;
            
    }
    
    
}

// 银行卡号验证

+ (BOOL) validCreditNumber:(NSString*)value{
    BOOL result = NO;
    NSInteger length = [value length];
    if (length >= 13) {
        
        if([self validatePureZeros:value]){
            
            return NO;
        }
        
        result = [self validatePureNum:value];
        
        if (result)
        {
            
            
            
            NSInteger digitValue = 0;
            NSInteger checkSum = 0;
            NSInteger index = 0;
            
            for (NSInteger i = 0 ; i < length; i ++  ) {
                
                index = (length -1) -i;
                digitValue = [[value substringWithRange:NSMakeRange(index, 1)] integerValue];
                
                if((i + 1)%2 != 0){ //1,3,5...
                    
                    checkSum += digitValue;
                    
                }else{ //2,4,6...
                    
                    digitValue = digitValue *2;
                    
                    if(digitValue >= 10){
                        
                        checkSum += digitValue - 9;
                        
                    }else{
                        
                        checkSum += digitValue;
                        
                    }
                }
            }
            
            result = (0 == checkSum%10) ? YES:NO;
        }
    }else {
        result = NO;
    }
    return result;
    
    
}


// 数值验证，最大值，最小值
+ (BOOL) validateNumRange:(NSString *) numStr withMax:(NSInteger )numMax withMin:(NSInteger) numMin{
    
    if(!numStr){
        
        if(numMin <= 0){
            
            return YES;
        }
        
        return NO;
    }
    
    
    NSInteger num = [numStr integerValue];
    if(num < numMin || num > numMax){
        
        return NO;
        
    }
    
    return YES;
    
    
    
}


// 验证字符串最大长度，最小长度
+ (BOOL) validateLenghLimit:(NSString*) value  withMin:(NSInteger) min{
    
    NSInteger length = [value length];
    
    if(length >= min){
        
        return YES;
        
    }
    
    return NO;
}

@end
